package com.csyf.manager.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: Enzo
 * @Date: 2018/4/17 17:46
 */
public class DateHelper {




    /**
     * 获取当前时间
     * @return
     */
    public static Date getNow(){
        return Calendar.getInstance().getTime();
    }


    /**
     * 获取年月日的字符串
     * @return
     */
    public static String getYMD(){
        return getYMD(getNow());
    }


    /**
     * 获取年月日的字符串
     * @return
     */
    public static String getYMdHms(){
        return getYMdHms(getNow());
    }

    public static String getYMdHms(Date date) {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
    }


    /**
     * 获取年月日的字符串
     * @return
     */
    public static String getYMD(Date date){
        return new SimpleDateFormat("yyyyMMdd").format(date);
    }

    /**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    public static Boolean isBelong(String startHours, String endHours){
        Boolean flag = false;

        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date now =null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse(startHours);
            endTime = df.parse(endHours);
        } catch (Exception e) {
            e.printStackTrace();
        }

        flag = belongCalendar(now, beginTime, endTime);
        return flag;
    }

    /**
     *
     */
    /**
     * 获取过去或者未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> getAnyDate(int intervals) {
        ArrayList<String> pastDaysList = new ArrayList<>();
//        ArrayList<String> fetureDaysList = new ArrayList<>();
//        for (int i = 0; i <intervals; i++) {
//            pastDaysList.add(getPastDate(i));
////            fetureDaysList.add(getFetureDate(i));
//        }

        for (int i = intervals-1; i >= 0; i--) {
            pastDaysList.add(getPastDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getAnyDate(7));;
    }

}
